package com.woniu.zsd.user.controller;


import com.woniu.form.AddAddressForm;
import com.woniu.form.UpdateAddressForm;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.user.entity.UserAddress;
import com.woniu.zsd.user.service.IUserAddressService;
import com.woniu.zsd.user.utils.UserTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@RestController
@RequestMapping("/user-address")
@Api(tags = "用户地址信息")
public class UserAddressController {
    @Autowired
    private UserTokenUtil tokenUtil;
    @Autowired
    private IUserAddressService userAddressService;
    @PostMapping("/addUserAddress")
    @ApiOperation(value = "用户新增地址")
    public Result addUserAddress(@ApiParam(name = "address", value = "用户地址") @RequestBody AddAddressForm aaf, @RequestHeader String token){
        String id = String.valueOf(tokenUtil.getUserId(token));
        UserAddress userAddress = userAddressService.addUserAddress(aaf,id);
        return Result.getResult(UserAddress.class).setData(userAddress).setMsg("新增地址成功");
    }
    @GetMapping("/selAllByUserId")
    @ApiOperation(value = "用户个人查询地址")
    public Result<List<UserAddress>> selAllByPage(@RequestHeader String token){
        String id = String.valueOf(tokenUtil.getUserId(token));
        List<UserAddress> list =userAddressService.selAll(id);
        return new Result<List<UserAddress>>().setCode(200).setMsg("查询用户成功").setData(list);
    }


    @PostMapping("/updateUserAddress")
    @ApiOperation(value = "用户修改地址")
    public Result updateUserAddress(@ApiParam(name = "uaf", value = "用户修改地址表单")@RequestBody UpdateAddressForm uaf, @RequestHeader String token){
        String id = String.valueOf(tokenUtil.getUserId(token));
        UserAddress userAddress = userAddressService.updateUserAddress(uaf,id);
        return Result.getResult(UserAddress.class).setData(userAddress).setMsg("修改地址成功");
    }

    @DeleteMapping("/deleteUserAddress")
    @ApiOperation(value = "用户删除地址")
    public Result deleteUserAddress(@ApiParam(name = "id", value = "地址id")@RequestParam String id,@RequestHeader String token){
        String userId = String.valueOf(tokenUtil.getUserId(token));
        boolean b = userAddressService.removeById(id);
        return Result.getResult(Boolean.class).setData(b).setMsg("删除地址成功");
    }

    @GetMapping("/selUserAddressById")
    @ApiOperation(value = "根据Id查询用户地址")
    public Result<UserAddress> selUserAddressById(@ApiParam(name = "id", value = "地址id")@RequestParam String id){
        UserAddress userAddress = userAddressService.getUserAddress(id);
        return Result.getResult(UserAddress.class).setData(userAddress).setMsg("查询地址成功");
    }


}
